<template>
	<div class="darkBg">
		<div class="userContent">
			<div class="cBox">
				<div class="cell">
					<div class="label">请选择头像</div>
					<div class="w70">
						<div class="ava">
							<button class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar"style="padding: 0; margin: 0;">
								<image v-if="userInfo.avatarUrl" class="avatar" :src="userInfo.avatarUrl" mode="widthFix"></image>
								<image v-else class="avatar" src="https://www.baixuemianshi.com/zhuanti/images/ava.png" mode="widthFix"></image>
							</button>
						</div>
					</div>
				</div>
				<div class="cell">
					<div class="label">请输入昵称</div>
					<div class="w70">
						<div class="nicknameBox">
							<input type="nickname" v-model="userInfo.nickname" @blur="blurname" placeholder="请输入昵称" />
						</div>
					</div>
				</div>
				<div class="">
					<u-button type="error" text="更新信息" @click="updateInfo()"></u-button>
				</div>
			</div>
			
		</div>
		
		<div class="popup" v-if="showPrivacy">
			<div class="popcontent">
				<view class="privacyTit">用户隐私保护提示</view>
				<div class="poptext">感谢您的使用，在使用本小程序前，应当阅读并同意<text
						@click="handleOpenPrivacyContract">《用户隐私保护指引》</text>。当您点击同意并开始使用程序服务时，即表示您已理解并同意该条款内容，该条款将对您产生法律约束力。如您拒绝，将无法进入小程序。
				</div>
				<div class="btns">
					<div class="btn"><u-button shape="circle" @tap="showPrivacy = false">不同意并退出</u-button></div>
					<div class="btn"><u-button type="error" shape="circle" id="agree-btn"
							open-type="agreePrivacyAuthorization"
							@click="handleAgreePrivacyAuthorization">同意并继续</u-button></div>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
	import {
		mapActions
	} from 'vuex';
	const db = wx.cloud.database()
	export default {
		data() {
			return {
				userInfo: {
					_id: "",
					phone: "",
					nickname: "",
					avatarUrl: ""
				},
				showPrivacy: false,
			};
		},
		onLoad(options) {
			if (wx.getPrivacySetting) {
				wx.getPrivacySetting({
					success: res => {
						if (res.needAuthorization) {
							this.showPrivacy = true
						} else {
						}
					},
					fail: () => {},
					complete: () => {}
				})
			}
		},
		computed: {
			userphone() {
				return this.$store.getters.userphone ? this.$store.getters.userphone : '';
			}
		},
		mounted() {
		},
		watch: {
			userphone(val) {
				if (this.userphone) {
					
				}
			}
		},

		created() {
			let that = this;
			that.getopenid()

		},
		methods: {
			...mapActions(['LogSetPhone', 'LogSetName']),
			onChooseAvatar(e) {
				let that = this
				if(!that.userInfo._id){
					uni.showToast({
						title:"请先登录！",
						icon: "error"
					})
					uni.navigateTo({
						url:"./login"
					})
					return
				}
				const {
					avatarUrl
				} = e.detail
				console.log(avatarUrl)
				uni.uploadFile({
					url: "https://www.baixuemianshi.com/zhuanti/upload_img/xcx2.php",
					filePath: e.detail.avatarUrl,
					name: 'file',
					formData: {
						user: 'test'
					},
					success: res => {
						let status = JSON.parse(res.data)
						if(status.status == 'success'){
							let link = 'https://www.baixuemianshi.com/zhuanti/upload_img/upload/' + status.data
							that.uploadAva(link)
						}else{
							uni.showToast({
								icon:"error",
								title:"上传失败"
							})
						}
						
					}
				})
			},
			uploadAva(link) {
				let that = this
				db.collection('all_user').doc(that.userInfo._id).update({
					data: {
						avatarUrl: link
					},
					success: res => {
						that.getUserDetail()
					}
				})
			},
			blurname(e) {
				let that = this
				db.collection('all_user').doc(that.userInfo._id).update({
					data: {
						nickname: e.detail.value
					},
					success: res => {
						that.getUserDetail()
					}
				})
			},
			handleAgreePrivacyAuthorization() {
				this.showPrivacy = false
			},
			handleOpenPrivacyContract() {
				// 打开隐私协议页面
				wx.openPrivacyContract({
					success: () => {}, // 打开成功
					fail: () => {}, // 打开失败
					complete: () => {}
				})
			},
			getopenid() {
				let that = this
				wx.cloud.callFunction({
					name: 'getopenid',
					data: {},
					complete: res => {
						wx.setStorageSync("useropenid", res.result.openid)
						that.openid = res.result.openid
						that.getUserDetail()
					}
				})
			},
			getUserDetail() {
				let that = this
				db.collection('all_user').where({
					_openid: that.openid
				}).get({
					success: res => {
						console.log(res)
						if (res.data.length > 0) {
							that.userInfo._id = res.data[0]._id
							that.userInfo.phone = res.data[0].phone
							that.userInfo.nickname = res.data[0].nickname
							that.userInfo.avatarUrl = res.data[0].avatarUrl
							that.LogSetPhone(that.userInfo.phone)
						}
					}
				})
			},
			updateInfo(){
				let pages = getCurrentPages();
				if (pages.length == 1) {
					uni.switchTab({
						url: '/pages/index/index'
					});
				} else {
					uni.navigateBack(1);
				}
			},
			contactus() {
				wx.makePhoneCall({
					phoneNumber: '18646042617'
				});
			},
			changePath(path) {
				let that = this;
				uni.navigateTo({
					url: path
				});
			},
			toLogin() {
				let path = './login??navTlt=' + '个人中心'
				this.changePath(path)
			},
			toWebview(link){
				console.log(link)
				uni.navigateTo({
					url: '../webview/index?href=' + encodeURIComponent(link)
				});
			}

		}
	};
</script>

<style>
	@import url(./index.css);
</style>